package servlet;

import domain.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/MenuSearchServlet")
public class MenuSearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求和响应的编码，防止乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        // 从request中获取搜索参数的值
        String searchfield = req.getParameter("textfield");
        // 初始化产品列表
        List<Product> ps = new ArrayList<Product>();
        // 调用Product类的search方法，得到产品列表ps
        ps = Product.search(searchfield); // Product类中的search方法返回查询到的产品列表
        // 获取session对象
        HttpSession session = req.getSession();

        // 将产品列表ps设置到session对象的ProductList参数中
        session.setAttribute("ProductList", ps);

        // 重定向请求至ProductList.jsp页面进行后续操作
        resp.sendRedirect(req.getContextPath()+"/WebContent/client/ProductList.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

}
